"""
pyecharts图表工具模块

该模块使用pyecharts库创建和渲染图表。
"""
from flask.cli import F
from pyecharts.charts import Bar, Pie, Line
import pyecharts.options as opts
from pyecharts.components import Table
from pyecharts.options import ComponentTitleOpts
from tools.utils_tools import only_one_id
MAIN_COLOR = "#EBF2FF"  # 主颜色
async def echarts_pair_url(data_pair: any, title: str) -> str:
    data_pair.sort(key=lambda x: x[1])
    id = only_one_id()
    # 创建饼图实例
    c = (
        Pie(init_opts=opts.InitOpts(bg_color=MAIN_COLOR))
        .add(
            series_name="访问来源",
            data_pair=data_pair,
            radius="55%",       # 设置饼图半径
            center=["50%", "50%"],  # 设置饼图中心位置
            label_opts=opts.LabelOpts(is_show=False, position="center"),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(
                title=title,
                pos_left="center",
                pos_top="20",
                title_textstyle_opts=opts.TextStyleOpts(color="#333"),
            ),
            toolbox_opts=opts.ToolboxOpts(
               feature=opts.ToolBoxFeatureOpts(
                    data_view=opts.ToolBoxFeatureDataViewOpts(
                        is_show=False,  # 显示数据视图
                    ),
                    restore=opts.ToolBoxFeatureRestoreOpts(
                        is_show=False,  # 显示重置按钮
                    ),
                    data_zoom=opts.ToolBoxFeatureDataZoomOpts(
                        is_show=False,  # 显示数据缩放按钮 
                    ),
                    magic_type=opts.ToolBoxFeatureMagicTypeOpts(
                        is_show=False,
                    ),
                    save_as_image=opts.ToolBoxFeatureSaveAsImageOpts(background_color="#333")  # 设置背景颜色为白色
               ) 
            ),
            legend_opts=opts.LegendOpts(is_show=False),  # 隐藏图例
        )
        .set_series_opts(
            tooltip_opts=opts.TooltipOpts(
                trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
            ),
            label_opts=opts.LabelOpts(color="#fff"),
        )
    )
    # 渲染图表到HTML文件
    c.render('./render_html/'+ id +'.html')
    return 'http://localhost:5000/render_html/'+ id +'.html'


async def echarts_bar_url(x_data: any,y_data: any, title: str) -> str:
    id = only_one_id()
    c = (
        Bar(init_opts=opts.InitOpts(bg_color=MAIN_COLOR))
        .add_xaxis(x_data)
        .set_global_opts(
            legend_opts=opts.LegendOpts(textstyle_opts=opts.TextStyleOpts(color="#333")),  # 隐藏图例
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15,color="#333")),
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(color="#333")),
            toolbox_opts=opts.ToolboxOpts(
               feature=opts.ToolBoxFeatureOpts(
                    data_view=opts.ToolBoxFeatureDataViewOpts(
                        is_show=False,  # 显示数据视图
                    ),
                    restore=opts.ToolBoxFeatureRestoreOpts(
                        is_show=False,  # 显示重置按钮
                    ),
                    data_zoom=opts.ToolBoxFeatureDataZoomOpts(
                        is_show=False,  # 显示数据缩放按钮 
                    ),
                    magic_type=opts.ToolBoxFeatureMagicTypeOpts(
                        type_=["bar", "line"],  # 显示柱状图和折线图
                    ),
                    save_as_image=opts.ToolBoxFeatureSaveAsImageOpts(background_color="#fff")  # 设置背景颜色为白色
               ) 
            ),
            tooltip_opts=opts.TooltipOpts(
                is_show=True, trigger="axis", axis_pointer_type="shadow"
            ),
            title_opts=opts.TitleOpts(title=title,title_textstyle_opts=opts.TextStyleOpts(color="#333")),
        )
    )
    for i in range(len(y_data)):
        c.add_yaxis(
            y_data[i]['name'],
            y_data[i]['data'], 
            label_opts=opts.LabelOpts(is_show=False),
        )
    # 渲染图表到HTML文件
    c.render('./render_html/'+ id +'.html')
    return 'http://localhost:5000/render_html/'+ id +'.html'

async def echarts_line_url(x_data:any, y_data: any, title: str) -> str:
    id = only_one_id()
    c = (
        Line(init_opts=opts.InitOpts(bg_color=MAIN_COLOR))
        .add_xaxis(x_data)
        .set_global_opts(
            legend_opts=opts.LegendOpts(textstyle_opts=opts.TextStyleOpts(color="#333")),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15,color="#333")),
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(color="#333")),
            toolbox_opts=opts.ToolboxOpts(
               feature=opts.ToolBoxFeatureOpts(
                    data_view=opts.ToolBoxFeatureDataViewOpts(
                        is_show=False,  # 显示数据视图
                    ),
                    restore=opts.ToolBoxFeatureRestoreOpts(
                        is_show=False,  # 显示重置按钮
                    ),
                    data_zoom=opts.ToolBoxFeatureDataZoomOpts(
                        is_show=False,  # 显示数据缩放按钮 
                    ),
                    magic_type=opts.ToolBoxFeatureMagicTypeOpts(
                        type_=["bar", "line"],  # 显示柱状图和折线图
                    ),
                    save_as_image=opts.ToolBoxFeatureSaveAsImageOpts(background_color="#333")  # 设置背景颜色为白色
               ) 
            ),
            title_opts=opts.TitleOpts(title=title,title_textstyle_opts=opts.TextStyleOpts(color="#333")),
        )
    )
    for i in range(len(y_data)):
        c.add_yaxis(
            y_data[i]['name'],
            y_data[i]['data'], 
        )
    # 渲染图表到HTML文件
    c.render('./render_html/'+ id +'.html')
    return 'http://localhost:5000/render_html/'+ id +'.html'

async def echarts_table_url(headers:any, rows: any, title: str) -> str:
    
    id = only_one_id()
    c = (
        Table(page_title=title)
        .add(headers, rows, attributes={ "style": "background-color:{MAIN_COLOR};color:#333; width: 880px;height: 450px", "border": "1px solid #333" })
        .set_global_opts(
            title_opts=ComponentTitleOpts(title=title)
        )
    )
    # 渲染图表到HTML文件
    c.render('./render_html/'+ id +'.html')
    return 'http://localhost:5000/render_html/'+ id +'.html'